<template>
  <label :class="wrapClasses">
    <span class="c-radio-item">
      <input
        type="radio"
        :class="inputClasses"
        :disabled="disabled"
        :checked="currentValue"
        :name="name"
        @change="change" />
    </span>
    <slot><span v-if="showSlot">{{ label }}</span></slot>
  </label>
</template>

<script type="text/ecmascript-6">
export default {
  name: 'RadioItem',
  props: {
    label: {
      type: [String, Number, Boolean]
    },
    disabled: {
      type: Boolean,
      default: false
    },
    name: {
      type: String
    }
  },
  computed: {
    wrapClasses () {
      return [
        'mixcheck-item',
        'c-radio-wrapper',
        {
          'c-wrapper-checked': this.currentValue,
          'c-wrapper-disabled': this.disabled
        }
      ]
    },
    inputClasses () {
      return 'c-mix-input'
    }
  },
  methods: {
    change (event) {
      if (this.disabled) return false
      const checked = event.target.checked
      this.currentValue = checked
      if (checked) this.$parent.change(this.label)
      else this.$parent.change('')
    }
  },
  data () {
    return {
      model: null,
      showSlot: true,
      currentValue: false
    }
  },
  mounted () {
    this.showSlot = this.$slots.default === undefined
    this.$parent.updateModel(true)
  }
}
</script>
